package com.hujing;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Author : hujing
 * @Date: 2025/6/17 10:59
 * @Description:
 */
@Configuration
public class ThreadPoolConfig {
    
    @Bean("taskExecutor")
    public ThreadPoolTaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        // 设置核心线程数
        taskExecutor.setCorePoolSize(3);
        // 设置最大线程数
        taskExecutor.setMaxPoolSize(5);
        // 设置队列容量
        taskExecutor.setQueueCapacity(100);
        // 设置线程前缀
        taskExecutor.setThreadNamePrefix("Async-");
        // 设置线程池等待时间,单位:秒
        taskExecutor.setKeepAliveSeconds(60);
        // 设置线程池拒绝策略
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化线程池 可以交由Spring来初始化
//        taskExecutor.initialize();
        
        return taskExecutor;
    }
}
